package com.projeto.business.controller;

import javax.ejb.Stateless;
import javax.persistence.Query;

import com.projeto.infra.business.GenericBean;
import com.projeto.persistence.Customer;
import com.projeto.persistence.Receipt;
import com.projeto.persistence.ReceiptDetail;
import com.projeto.persistence.User;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;


@Stateless
public class ReceiptController extends GenericBean<Receipt> {

    /* (non-Javadoc)
     * @see com.novo.projeto.business.IUserBean#find(java.lang.String)
     */
    public Receipt find(String id) {
        return getEntityManager().find(Receipt.class, id);
    }

    /* (non-Javadoc)
     * @see com.novo.projeto.business.IUserBean#findAll()
     */
    @SuppressWarnings("unchecked")
    @Override
    public List<Receipt> findAll() {
        Query query = em.createQuery("SELECT e FROM Receipt e");
        return (List<Receipt>) query.getResultList();
    }

    /**
     *
     * @return
     */
    @SuppressWarnings("unchecked")
    public List<Receipt> findAllActive() {
        Query query = em.createQuery("SELECT e FROM Receipt e WHERE e.inactive = 0");
        return (List<Receipt>) query.getResultList();
    }
    
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    @Override
    public void save(Receipt receipt) {

        String userLogin = ctx.getCallerPrincipal().getName();
        User user = em.find(User.class, userLogin);
        Date updatedDt = new Date();
        if (receipt == null) {
            throw new IllegalArgumentException("Receipt cannot be null");
        }

        if (receipt.getId() == null || "".equals(receipt.getId()))
        {
            receipt.setId(UUID.randomUUID().toString());
        }
        
        Receipt temp = em.find(Receipt.class, receipt.getId() != null ? receipt.getId() : "");
        if (temp == null) {
            temp = receipt;
            temp.setId(UUID.randomUUID().toString());
            //temp.setCreateDt(updatedDt);
            //temp.setCreatedBy(user);
        }
        
        temp.setDescription(receipt.getDescription());
        temp.setPickup(receipt.getPickup());
        temp.setReceivedBy(receipt.getReceivedBy());        
        temp.setShipper(receipt.getShipper());
        temp.setDeliveredBy(receipt.getDeliveredBy());
        temp.setConsignee(receipt.getConsignee());
        temp.setReceiptDetails(receipt.getReceiptDetails());
        temp.setUpdatedBy(user);
        temp.setUpdateDt(updatedDt);

        //List<ReceiptDetail> details = new ArrayList<ReceiptDetail>();
        if (temp.getReceiptDetails() != null && !temp.getReceiptDetails().isEmpty())
        {
            for(ReceiptDetail gt: temp.getReceiptDetails())
            {
                String receiptDetailId = gt.toString();
                System.out.println("DETAILID: " + receiptDetailId);

                //ReceiptDetail g = em.find(ReceiptDetail.class, receiptDetailId);
                gt.setReceipt(temp);
            }
        }
        //temp.setReceiptDetails(details);
 
        // @TODO insert logic to  add Events
        //em.persist(receipt);
        super.save(temp);
        System.out.println(receipt.toString());
    }

    @Override
    protected boolean validate(Receipt e) {
        return true;
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
}
